package com.cn.wanxi.servlet;

import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.service.CompanyService;
import com.cn.wanxi.service.NewsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 当前的类对应jsp页面
 */
@WebServlet("/home")
public class HomeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(111);
//        String logo = "../img/logo.jpg";
//        String phone = "13999999999";
//        req代表容器：第一个参数代表记号（key值），第二个参数代表货物，（value值）
//        req.setAttribute("logo", logo);
//        req.setAttribute("phone", phone);
//        CompanyService companyService = new CompanyService();
//        CompanyModel companyModel = companyService.setCompanyModel();
//        req.setAttribute("companyModel", companyModel);
//        req.getSession().setAttribute("companyModel", companyModel);
//        req.getServletContext().setAttribute("companyModel", companyModel);
        CommonServlet.setCommonData(req);
        //找到Java后台后，后台响应给前端一个页面，顺便带点数据给前端
        /*
         * 新闻信息
         * 1.创建service层
         * 2.得到数据
         * 3.将数据放入req
         */
        NewsService newsService = new NewsService();
        NewsModel[] newsModels = newsService.getNewsModelsForHomePage();
        req.setAttribute("newsModels", newsModels);
        req.getRequestDispatcher("/jsp/home.jsp").forward(req, resp);
//        找到Java后台，直接响应给前端一个页面，什么都不做
//        resp.sendRedirect("/jsp/home.jsp");
    }
}
